package com.zxk.blog.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxk.api.service.NewsService;
import com.zxk.blog.feign.CrawlerEsFeignService;
import com.zxk.blog.service.EsDataRestTemplateService;
import com.zxk.core.common.model.News;
import com.zxk.core.common.util.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("新闻对外接口")
@RestController
@RequestMapping("/blog/api/news")
public class NewsController {


    @Autowired
    private NewsService newsService;

    @Autowired
    private CrawlerEsFeignService crawlerEsFeignService;

    @Autowired
    private EsDataRestTemplateService esDataRestTemplateService;


    @ApiOperation(value="查找所有新闻，一般不要使用",notes="查找所有新闻")
    @PostMapping(value = "/all-news")
    public JsonData allNews(){
        List<News> newsList = newsService.findAll();
        return JsonData.buildSuccess(newsList,200);
    }

    @ApiOperation(value="查找新闻，分页",notes="查找新闻，分页")
    @PostMapping("/find-news-page")
    public JsonData findNewsByPage(@RequestParam Integer pageNum){
        IPage<News> newsIPage = newsService.find(pageNum, null);
        return JsonData.buildSuccess(newsIPage,200);
    }
//后端重定向
//    @RequestMapping("/add-fund")
//    public String addFund(Fund fund){
//        this.fundService.save(fund);
//        return "redirect:/fund/analysise/fund-list";
//    }


    @ApiOperation(value="查找新闻，按时间",notes="查找新闻，按时间")
    @PostMapping("/fund-order-by-time")
    public JsonData findByTime(@RequestParam String time,@RequestParam Integer pageNum){
//        List<Fund> funds = this.fundService.selectOrderFundByCreateTime();
        IPage<News> newsIPage = newsService.findByTime(time,pageNum);
        return JsonData.buildSuccess(newsIPage,200);
    }

    @ApiOperation(value="查找新闻，模糊查询",notes="查找新闻，模糊查询")
    @PostMapping("/fund-analysis-time")
    public JsonData findByLike(@RequestParam String title,@RequestParam Integer pageNum){
        IPage<News> newsIPage = newsService.findByLike(title,pageNum);
        JsonData jsonData = new JsonData(200,newsIPage,"查询成功");
        return jsonData;
    }


//    @ApiOperation(value="es数据接口",notes="es数据接口")
//    @PostMapping("/get/{index}/{field}/{keyword}/{pageNo}/{pageSize}")
//    public JsonData getEsDada( @PathVariable(value = "index") String index,
//                               @PathVariable(value = "field") String field,
//                               @PathVariable(value = "keyword") String keyword,
//                               @PathVariable(value = "pageNo") Integer pageNo,
//                               @PathVariable(value = "pageSize") Integer pageSize){
//        JsonData esDada = crawlerEsFeignService.getEsDada(index, field, keyword, pageNo, pageSize);
//        return JsonData.buildSuccess(esDada,200);
//    }
//
//
//    @ApiOperation(value="es数据接口使用restTemplate",notes="es数据接口使用restTemplate")
//    @PostMapping("/getEs/{index}/{field}/{keyword}/{pageNo}/{pageSize}")
//    public JsonData getEsDadaRestTemplate( @PathVariable(value = "index") String index,
//                               @PathVariable(value = "field") String field,
//                               @PathVariable(value = "keyword") String keyword,
//                               @PathVariable(value = "pageNo") Integer pageNo,
//                               @PathVariable(value = "pageSize") Integer pageSize){
//        JsonData esDada = esDataRestTemplateService.esDataProvider(index, field, keyword, pageNo, pageSize);
//        return JsonData.buildSuccess(esDada,200);
//    }
}
